<!DOCTYPE html>
<html>
<head>
    <title>Electromagnetic Field Diagram</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        canvas {
            border: 1px solid #ccc;
        }
    </style>
</head>
<body>
    <canvas id="physicsCanvas" width="600" height="450"></canvas>
    <script>
        const canvas = document.getElementById('physicsCanvas');
        const ctx = canvas.getContext('2d');

        // Clear canvas with a white background
        ctx.fillStyle = 'white';
        ctx.fillRect(0, 0, canvas.width, canvas.height);

        // --- Drawing parameters ---
        const railYTop = 100;
        const railYBottom = 300;
        const railXLeft = 80;
        const railXRight = 550;
        const rodX = 420;
        const mainLineWidth = 2;

        // --- Helper function to draw an arrow ---
        function drawArrow(ctx, fromX, fromY, toX, toY, headLength = 10) {
            const dx = toX - fromX;
            const dy = toY - fromY;
            const angle = Math.atan2(dy, dx);
            ctx.beginPath();
            ctx.moveTo(fromX, fromY);
            ctx.lineTo(toX, toY);
            ctx.lineTo(toX - headLength * Math.cos(angle - Math.PI / 6), toY - headLength * Math.sin(angle - Math.PI / 6));
            ctx.moveTo(toX, toY);
            ctx.lineTo(toX - headLength * Math.cos(angle + Math.PI / 6), toY - headLength * Math.sin(angle + Math.PI / 6));
            ctx.stroke();
        }

        // 1. Draw the magnetic field (X symbols)
        ctx.strokeStyle = '#888';
        ctx.lineWidth = 1;
        ctx.font = '20px sans-serif';
        const gridSize = 50;
        const xSize = 10;
        for (let x = gridSize / 2; x < canvas.width; x += gridSize) {
            for (let y = gridSize / 2; y < 380; y += gridSize) {
                ctx.beginPath();
                ctx.moveTo(x - xSize / 2, y - xSize / 2);
                ctx.lineTo(x + xSize / 2, y + xSize / 2);
                ctx.moveTo(x + xSize / 2, y - xSize / 2);
                ctx.lineTo(x - xSize / 2, y + xSize / 2);
                ctx.stroke();
            }
        }

        // --- Main drawing starts here ---
        ctx.strokeStyle = 'black';
        ctx.lineWidth = mainLineWidth;

        // 2. Draw the rails and resistor
        // Top rail
        ctx.beginPath();
        ctx.moveTo(railXLeft, railYTop);
        ctx.lineTo(railXRight, railYTop);
        ctx.stroke();

        // Bottom rail
        ctx.beginPath();
        ctx.moveTo(railXLeft, railYBottom);
        ctx.lineTo(railXRight, railYBottom);
        ctx.stroke();

        // Resistor R
        const resistorWidth = 20;
        const resistorHeight = 60;
        const resistorY = (railYTop + railYBottom - resistorHeight) / 2;
        
        // Connecting wires to resistor
        ctx.beginPath();
        ctx.moveTo(railXLeft, railYTop);
        ctx.lineTo(railXLeft, resistorY);
        ctx.stroke();
        
        ctx.beginPath();
        ctx.moveTo(railXLeft, railYBottom);
        ctx.lineTo(railXLeft, resistorY + resistorHeight);
        ctx.stroke();

        // Resistor box
        ctx.strokeRect(railXLeft - resistorWidth / 2, resistorY, resistorWidth, resistorHeight);

        // 3. Draw the conducting rod MN
        ctx.beginPath();
        ctx.moveTo(rodX, railYTop);
        ctx.lineTo(rodX, railYBottom);
        ctx.stroke();
        
        // Hooks on the rod
        const hookRadius = 8;
        ctx.lineWidth = 1.5;
        // Top hook (at M)
        ctx.beginPath();
        ctx.arc(rodX, railYTop, hookRadius, Math.PI / 2, 3 * Math.PI / 2, false);
        ctx.stroke();
        
        // Bottom hook (at N)
        ctx.beginPath();
        ctx.arc(rodX, railYBottom, hookRadius, Math.PI / 2, 3 * Math.PI / 2, false);
        ctx.stroke();

        // 4. Draw the velocity vector v
        ctx.lineWidth = mainLineWidth;
        const velocityY = (railYTop + railYBottom) / 2;
        drawArrow(ctx, rodX, velocityY, rodX + 100, velocityY);

        // 5. Add labels
        ctx.fillStyle = 'black';
        ctx.font = 'italic 24px Times New Roman';
        ctx.fillText('R', railXLeft - 40, velocityY + 8);
        ctx.fillText('v', rodX + 45, velocityY - 10);

        ctx.font = '22px Times New Roman';
        ctx.fillText('M', rodX - 8, railYTop - 15);
        ctx.fillText('N', rodX - 8, railYBottom + 25);

        // Add bottom text
        ctx.font = '22px "Microsoft YaHei", "SimSun", sans-serif';
        ctx.textAlign = 'center';
        ctx.fillText('习题 9-9 图', canvas.width / 2, canvas.height - 20);

    </script>
</body>
</html>